package service

import (
	"blog_demo/dao"
	"blog_demo/models"
	"blog_demo/utils"
	"errors"
)

func Login(userName, passwd string) (*models.LoginRes, error) {
	passwd = utils.Md5Crypt(passwd, "wrz")
	user := dao.GetUser(userName, passwd)
	if user == nil {
		return nil, errors.New("账号密码错误")
	}
	uid := user.Uid
	// jwt
	token, err := utils.Award(&uid)
	if err != nil {
		return nil, errors.New("token生成失败")
	}
	userInfo := models.UserInfo{
		Uid:      user.Uid,
		UserName: user.UserName,
		Avatar:   user.Avatar,
	}
	loginResponse := &models.LoginRes{
		Token:    token,
		UserInfo: userInfo,
	}
	return loginResponse, nil
}
